#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2018/7/31 10:24
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 371. 两整数之和.py
# @Software: PyCharm


class Solution:
    def getSum(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: int
        """
        print('a,b:', bin(a), bin(b))

        def plus(x, y):
            print('plus:', x, y)
            print(' ', bin(x), bin(y))
            x, y = x ^ y, x & y
            while y:
                print(' ', bin(x), bin(y))
                y <<= 1
                x, y = x ^ y, x & y
            print('end:', x, y)
            return x
        if a < 0 and b < 0 or a >= 0 and b >= 0:
            return plus(a, b) if a >= 0 else -plus(-a, -b)
        else:
            if a < 0:
                a, b = b, a
            b = -b
            print(a, '-', b)
            print(a ^ b)
            print(bin(a), bin(b))
            if a < b:
                si = -1
            else:
                si = 1
            return si * (a ^ b)


def run(a, b):
    s = Solution()
    print(a, '+', b)
    print(s.getSum(a, b))


if __name__ == '__main__':
    questions = [
        {
            'a': 2,
            'b': 3
        },
        {
            'a': -1,
            'b': 1
        },
        {
            'a': 0,
            'b': 17
        },
        {
            'a': -12,
            'b': -8
        },
        {
            'a': -14,
            'b': 16
        }
    ]
    for q in questions:
        run(q['a'], q['b'])

    a = 1
    b = 1
    # print('%d,%d: %d, %d' % (a, b, bin(a), bin(b)))
    print('a,b...')
    print(a ^ b)
    print(16 ^ -14)
    print(bin(1), bin(~1), bin(-1))
    print('%u,%u' % (1, -1))
    print(bin((-1) & 0xffffffff))
    print(~1)
    print(~ (1 & 1) & (1 & 1))

    def _01(x, y):
        return ~ (x & 1) & (y & 1)
    print(_01(10, 3))

    def sub(x, y):
        p = 0
        i = 1
        ans = 0
        while x:
            a = ~ (x & 1) & (y & 1)
            if ~ (x & 1) & (y & 1):
               ans |= i
               p = 1
            else:
                ans |= i & ((x & 1) ^ (y & 1))
            x, y, i = x >> 1, y >> 1
